package com.example.yuanann.vegetable_app.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.yuanann.vegetable_app.MainApplication;
import com.example.yuanann.vegetable_app.R;
import com.example.yuanann.vegetable_app.constant.UserTypeEnum;
import com.example.yuanann.vegetable_app.repository.AppDatabase;
import com.example.yuanann.vegetable_app.repository.dao.UserDao;
import com.example.yuanann.vegetable_app.repository.entity.User;
import com.example.yuanann.vegetable_app.util.GsonUtil;
import com.example.yuanann.vegetable_app.util.LogUtil;

import cn.hutool.core.util.StrUtil;
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.schedulers.Schedulers;

public class Register extends AppCompatActivity {
    Button bu1, goLoginBtn;
    EditText ed1,ed2,ed3;
    String name,psw,pswagain;

    RadioGroup chooseUserTypeGroup;

    /**
     * normal：普通用户
     * merchant: 商户
     */
    private String userType = "normal";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        bu1 = (Button) findViewById(R.id.bu1);
        ed1=(EditText) findViewById(R.id.ed1);
        ed2=(EditText) findViewById(R.id.ed2);
        ed3=(EditText) findViewById(R.id.ed3);
        goLoginBtn = (Button) findViewById(R.id.goLoginButton);
        chooseUserTypeGroup = findViewById(R.id.radioChooseUserTypeGroup);
        goLoginBtn.setOnClickListener((view) -> {
            Intent intent = new Intent();
            intent.setClass(Register.this, Login.class);
            startActivity(intent);
        });
        bu1.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                name = ed1.getText().toString().trim();
                psw= ed2.getText().toString().trim();
                pswagain= ed3.getText().toString().trim();
                if (TextUtils.isEmpty(name)) {
                    Toast.makeText( Register.this, "请输入用户名", Toast.LENGTH_SHORT).show();
                } else if (TextUtils.isEmpty(psw)) {
                    Toast.makeText( Register.this, "请输入密码", Toast.LENGTH_SHORT).show();
                } else if (TextUtils.isEmpty(pswagain)) {
                    Toast.makeText( Register.this,"请再次输入密码", Toast.LENGTH_SHORT).show();
                } else if (!psw.equals(pswagain)) {
                    Toast.makeText(Register.this, "输入两次的密码不一样", Toast.LENGTH_SHORT).show();
                }else if(StrUtil.isBlankIfStr(userType)){
                    Toast.makeText(Register.this, "请选择用户类型", Toast.LENGTH_SHORT).show();
                }else if(isExistName(name)){
                    Toast.makeText(Register.this, "此账户名已经存在", Toast.LENGTH_SHORT).show();
                } else {
                    AppDatabase db = AppDatabase.getDatabase(MainApplication.getContext());
                    UserDao userDao = db.userDao();
                    User user = new User();
                    user.setUserName(name);
                    user.setPassword(psw);
                    user.setUserType(userType);
                    userDao.insert(user)
                            .subscribeOn(Schedulers.single())
                            .observeOn(AndroidSchedulers.mainThread())
                            .subscribe(()->{
                                LogUtil.info("注册成功");
                                Toast.makeText(Register.this, "注册成功", Toast.LENGTH_SHORT).show();
                                SharedPreferences sp=getSharedPreferences("loginInfo", MODE_PRIVATE);
                                SharedPreferences.Editor editor=sp.edit();
                                editor.putString(name, psw);
                                editor.putString("username",name);
                                editor.commit();
                                Intent data = new Intent();
                                setResult(RESULT_OK, data);
                                Register.this.finish();
                            },throwable -> {
                                LogUtil.info("注册失败");
                                Toast.makeText(Register.this, "注册失败", Toast.LENGTH_SHORT).show();
                            });

                }
            } });

        chooseUserTypeGroup.setOnCheckedChangeListener((group, checkedId) -> {
            if(checkedId == R.id.radioNormalUser){
                // 普通用户
                this.userType = "normal";
            }else{
                // 商户
                this.userType = "merchant";
            }
        });
    }
    private boolean isExistName(String sname){
        boolean has_name=false;
        SharedPreferences sp=getSharedPreferences("loginInfo", MODE_PRIVATE);
        String spsw=sp.getString(name, "");//传入用户名获取密码
        if(!TextUtils.isEmpty(spsw)) {
            has_name=true;
        }
        return has_name;
    }
    private void saveRegister(String name, String psw, String userType){

    }
}

